'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 远征商城购买
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var rewardService = require('../../../../services/rewardService');
var dataApi = require('../../../../libs/dataApi.js');
var vipConf = require('../../../../config/vipConf.js');
var expeditionConf = require('../../../../config/expeditionConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var shop_pos = msg.shop_pos;

    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['expedition', 'role', 'build', 'bag', 'accrued'], cb);
            },
            function (cb) {
                var expedition_model = player_manager.expedition;
                var role_model = player_manager.role;
                var monster_open_lvl = constantUtil.value('monster_open_lvl');
                if (role_model.lv < monster_open_lvl) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                var shop_item = expedition_model.getShopItem(shop_pos);
                if (!shop_item) {
                    return next(null, {
                        code: Code.EXPEDITION_INVLID_SHOP_POS
                    });
                }

                if (!expedition_model.decShopItemCount(shop_pos)) {
                    return next(null, {
                        code: Code.EXPEDITION_SHOP_ITEM_BUY_LIMIT,
                    });
                }

                var shop_item_conf = expeditionConf.getShopItem(shop_item.shop_id);
                if (!shop_item_conf) {
                    return next(null, {
                        code: Code.EXPEDITION_INVLID_SHOP_ITEM
                    });
                }

                if (!expedition_model.decExpeditionCoin(shop_item_conf.cost_monster_coin)) {
                    return next(null, {
                        code: Code.EXPEDITION_NOT_ENOUGH_COIN
                    });
                }

                var reward_info = {
                    reward: shop_item_conf.reward,
                    reward_type: shop_item_conf.reward_type,
                    reward_num: shop_item_conf.reward_num,
                    reward_value: shop_item_conf.reward_value,
                    reward_hole_num: shop_item_conf.reward_hole_num
                };
                rewardService.addRewardToRole(player_manager, reward_info, moduleType.MDL_EXPEDITION, operationType.OPR_EXPEDITION_SHOP_BUY);
                reward_list.push(reward_info);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
